package com.htfyun.factorytest;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.provider.Settings;

import com.htfyun.factorytest.common.Constants;
import com.htfyun.factorytest.common.FTUtils;
import com.htfyun.factorytest.config.EinkProperty;
import com.htfyun.factorytest.utils.DebugLog;
import com.htfyun.factorytest.utils.ReflectUtils;
import com.htfyun.factorytest.utils.executor.AppExecutors;

/**
 * Created by sst on 2018/5/30.
 */

public class BootCompletedReceiver extends BroadcastReceiver implements Runnable {

    private final static String TAG = "BootCompletedReceiver";
    private Context mContext;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        final String action = intent.getAction();

        DebugLog.e("~~~~~ Congratulations: We receive BootCompletedReceiver");

        if (action != null && action.equals(Intent.ACTION_BOOT_COMPLETED)) {
            AppExecutors.runOnIoThread(this);
        }
    }

    @Override
    public void run() {

        final boolean isPvRoEinkFTDone = EinkProperty.isPvRoEinkFTDone();
        final boolean isFTPassed = FTUtils.isFTPassed();
        DebugLog.e("~~~~~ Congratulations: We receive BootCompletedReceiver isPvRoEinkFTDone = "
                + isPvRoEinkFTDone
                + ", isFTPassed = " + isFTPassed);

//        if (isPvRoEinkFTDone && isFTPassed) {
//            DebugLog.e(" PvRoEinkFTDone and FTPassed, return directly.");
//            return;
//        }
        if (isPvRoEinkFTDone) {
            if (EinkProperty.isR2()) {
                setAppEnabled(Constants.DISABLE_PACKAGE_NAMES, true);
            }
            return;
        }
        if (EinkProperty.isJyt()) {
            try {
                int powerOffTimeout = Settings.System.getInt(mContext.getContentResolver(), Constants.POWER_OFF_TIMEOUT);
                if (powerOffTimeout != Integer.MAX_VALUE) {
                    Settings.System.putInt(mContext.getContentResolver(),
                            Constants.POWER_OFF_TIMEOUT, Integer.MAX_VALUE);
                }
            } catch (Exception e) {
                DebugLog.e(e.getMessage());
            }
        }
        FTUtils.startFTFloat(RootApplication.getContext());
        FTUtils.startMainActivity(RootApplication.getContext());
        if (EinkProperty.isSuper() || EinkProperty.isR2()) {
            setAppEnabled(Constants.DISABLE_PACKAGE_NAMES, false);
            AppExecutors.postToMainThreadDelay(() -> FTUtils.startMainActivity(RootApplication.getContext()), 3000);
        }
    }

    private void setAppEnabled(String[] packNames, boolean enabled) {
        try {
            for (String packName : packNames) {
                //disable
                ReflectUtils.invokeMethod(
                        mContext.getPackageManager(),
                        "setApplicationEnabledSetting",
                        packName,
                        enabled ? Constants.COMPONENT_ENABLED_STATE_ENABLED : Constants.COMPONENT_ENABLED_STATE_DISABLED,
                        PackageManager.DONT_KILL_APP);
                if (enabled) {
                    return;
                }
                //force-stop
                ActivityManager mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
                ReflectUtils.invokeMethod(
                        mActivityManager,
                        "forceStopPackageAsUser",
                        packName,
                        ReflectUtils.invokeMethod("android.app.ActivityManager", "getCurrentUser"));
            }
        } catch (Exception e) {
            DebugLog.e(e.toString());
            e.printStackTrace();
        }
    }

}
